package com.netx.generics.util;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import com.netx.generics.collections.ImmutableList;

// TODO
// Change name to Document? see Substance
// Put this together with Files?
public class Text {

	private final List<String> _lines;

	public Text(Reader in) throws IOException {
		BufferedReader reader = null;
		if(!(in instanceof BufferedReader)) {
			reader = new BufferedReader(in);
		}
		else {
			reader = (BufferedReader)in;
		}
		_lines = new ArrayList<String>();
		String line = reader.readLine();
		while(line != null) {
			_lines.add(line);
			line = reader.readLine();
		}
		in.close();
	}
	
	public int countLines() {
		return _lines.size();
	}

	public String getLine(int index) {
		return (String)_lines.get(index);
	}
	
	public ImmutableList<String> getLines() {
		return new ImmutableList<String>(_lines);
	}
}
